﻿using System;
using System.Data;
using System.Data.SqlClient;

namespace HRMService
{
    public partial class MainService : IAdvancePayment
    {
        public DataSet AdvancePayment_GetAll()
        {
            DataSet retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_AdvancePayment_GetAll", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            try
            {
                retVal = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                da.Fill(retVal);
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }

        public DataSet AdvancePayment_GetList(out int ItemCount, int currentPage, int pageSize, int BranchID,int DepartmentID, string keyword, int _month, int _year)
        {
            DataSet retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_AdvancePayment_GetList", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@CurrentPage", currentPage);
            dbCmd.Parameters.AddWithValue("@PageSize", pageSize);
            dbCmd.Parameters.AddWithValue("@BranchID", BranchID);
            dbCmd.Parameters.AddWithValue("@DepartmentID", DepartmentID);
            dbCmd.Parameters.AddWithValue("@keyword", keyword);
            dbCmd.Parameters.AddWithValue("@month", _month);
            dbCmd.Parameters.AddWithValue("@year", _year);
            try
            {
                retVal = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                da.Fill(retVal);
                ItemCount = Convert.ToInt32(retVal.Tables[1].Rows[0][0]);
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }

        public DataSet AdvancePayment_GetWithEmployee(int EmployeeID, int month, int year)
        {
            DataSet retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_AdvancePayment_GetWithEmployee", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@EmployeeID", EmployeeID);
            dbCmd.Parameters.AddWithValue("@month", month);
            dbCmd.Parameters.AddWithValue("@year", year);

            try
            {
                retVal = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                da.Fill(retVal);

            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }

        public bool AdvancePayment_CheckExistEmployee(int EmployeeID, int month, int year)
        {
            bool retVal = false;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_AdvancePayment_CheckEmployee", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@EmployeeID", EmployeeID);
            dbCmd.Parameters.AddWithValue("@month", month);
            dbCmd.Parameters.AddWithValue("@year", year);
            try
            {
                dbConn.Open();
                int Count = Convert.ToInt32(dbCmd.ExecuteScalar());
                retVal = Count > 0 ? true : false;
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }

        public AdvancePaymentInfo AdvancePayment_GetInfo(int AdvanceID)
        {
            AdvancePaymentInfo retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_AdvancePayment_GetInfo", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@AdvancePayment_ID", AdvanceID);
            SqlDataReader dr = null;
            try
            {
                dbConn.Open();
                dr = dbCmd.ExecuteReader();
                if (dr.Read())
                {
                    retVal = new AdvancePaymentInfo();
                    retVal.ID = Convert.ToInt32(dr["AdvancePayment_ID"]);
                    retVal.EmployeeID = Convert.ToInt32(dr["AdvancePayment_EmployeeID"]);
                    retVal.BranchID = Convert.ToInt32(dr["AdvancePayment_BranchID"]);
                    retVal.DepartmentID = Convert.ToInt32(dr["AdvancePayment_DepartmentID"]);
                    retVal.Position = dr["AdvancePayment_Position"].ToString();
                    retVal.Date = Convert.ToDateTime(dr["AdvancePayment_Date"]);
                    retVal.Amount = Convert.ToDecimal(dr["AdvancePayment_Amount"]);
                    retVal.AmountPayment = Convert.ToDecimal(dr["AdvancePayment_AmountPayment"]);
                    retVal.DinhKy = Convert.ToBoolean(dr["AdvancePayment_DinhKy"]);
                    retVal.Month = Convert.ToInt32(dr["AdvancePayment_Month"]);
                    retVal.Description = dr["AdvancePayment_Description"].ToString();
                }
            }
            finally
            {
                if (dr != null) dr.Close();
                dbConn.Close();
            }
            return retVal;
        }

        public AdvancePaymentInfo AdvancePayment_GetInfoEmployee(int EmployeeID)
        {
            AdvancePaymentInfo retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_AdvancePayment_GetInfoEmployee", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@EmployeeID", EmployeeID);
            SqlDataReader dr = null;
            try
            {
                dbConn.Open();
                dr = dbCmd.ExecuteReader();
                if (dr.Read())
                {
                    retVal = new AdvancePaymentInfo();
                    retVal.ID = Convert.ToInt32(dr["AdvancePayment_ID"]);
                    retVal.EmployeeID = Convert.ToInt32(dr["AdvancePayment_EmployeeID"]);
                    retVal.BranchID = Convert.ToInt32(dr["AdvancePayment_BranchID"]);
                    retVal.DepartmentID = Convert.ToInt32(dr["AdvancePayment_DepartmentID"]);
                    retVal.Position = dr["AdvancePayment_Position"].ToString();
                    retVal.Date = Convert.ToDateTime(dr["AdvancePayment_Date"]);
                    retVal.Amount = Convert.ToDecimal(dr["AdvancePayment_Amount"]);
                    retVal.AmountPayment = Convert.ToDecimal(dr["AdvancePayment_AmountPayment"]);
                    retVal.DinhKy = Convert.ToBoolean(dr["AdvancePayment_DinhKy"]);
                    retVal.Month = Convert.ToInt32(dr["AdvancePayment_Month"]);
                    retVal.Description = dr["AdvancePayment_Description"].ToString();
                }
            }
            finally
            {
                if (dr != null) dr.Close();
                dbConn.Close();
            }
            return retVal;
        }

        public int AdvancePayment_Insert(AdvancePaymentInfo eInfo)
        {
            int retVal = 0;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_AdvancePayment_Insert", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@AdvancePayment_EmployeeID", eInfo.EmployeeID);
            dbCmd.Parameters.AddWithValue("@AdvancePayment_BranchID", eInfo.BranchID);
            dbCmd.Parameters.AddWithValue("@AdvancePayment_DepartmentID", eInfo.DepartmentID);
            dbCmd.Parameters.AddWithValue("@AdvancePayment_Position",eInfo.Position);
            dbCmd.Parameters.AddWithValue("@AdvancePayment_Date", eInfo.Date);
            dbCmd.Parameters.AddWithValue("@AdvancePayment_Amount", eInfo.Amount);
            dbCmd.Parameters.AddWithValue("@AdvancePayment_AmountPayment", eInfo.AmountPayment);
            dbCmd.Parameters.AddWithValue("@AdvancePayment_DinhKy", eInfo.DinhKy);
            dbCmd.Parameters.AddWithValue("@AdvancePayment_Month", eInfo.Month);
            dbCmd.Parameters.AddWithValue("@AdvancePayment_Description", eInfo.Description);
            try
            {
                dbConn.Open();
                retVal = Convert.ToInt32(dbCmd.ExecuteScalar());
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        public bool AdvancePayment_Update(AdvancePaymentInfo eInfo)
        {
            bool retVal = false;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_AdvancePayment_Update", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@AdvancePayment_ID", eInfo.ID);
            dbCmd.Parameters.AddWithValue("@AdvancePayment_EmployeeID", eInfo.EmployeeID);
            dbCmd.Parameters.AddWithValue("@AdvancePayment_BranchID", eInfo.BranchID);
            dbCmd.Parameters.AddWithValue("@AdvancePayment_DepartmentID", eInfo.DepartmentID);
            dbCmd.Parameters.AddWithValue("@AdvancePayment_Position",eInfo.Position);
            dbCmd.Parameters.AddWithValue("@AdvancePayment_Date", eInfo.Date);
            dbCmd.Parameters.AddWithValue("@AdvancePayment_Amount", eInfo.Amount);
            dbCmd.Parameters.AddWithValue("@AdvancePayment_AmountPayment", eInfo.AmountPayment);
            dbCmd.Parameters.AddWithValue("@AdvancePayment_DinhKy", eInfo.DinhKy);
            dbCmd.Parameters.AddWithValue("@AdvancePayment_Month", eInfo.Month);
            dbCmd.Parameters.AddWithValue("@AdvancePayment_Description", eInfo.Description);
            try
            {
                dbConn.Open();
                if (dbCmd.ExecuteNonQuery() > 0)
                {
                    retVal = true;
                }
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }

        public bool AdvancePayment_Delete(int AdvanceID)
        {
            bool retVal = false;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_AdvancePayment_Delete", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@AdvancePayment_ID", AdvanceID);
            try
            {
                dbConn.Open();
                if (dbCmd.ExecuteNonQuery() > 0)
                {
                    retVal = true;
                }
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
    }
}

